﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BatchDelete
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private int progressBarValue;
        public int ProgressBarValue
        {
            get => progressBarValue;
            set
            {
                progressBarValue = value;
                OnPropertyChanged("ProgressBarValue");
                /*progressBar.Dispatcher.Invoke(new System.Action(() =>
                {
                    progressBar.Value = value;
                }));*/
            }
        }

        private int progressBarMax;
        public int ProgressBarMax
        {
            get => progressBarMax;
            set
            {
                progressBarMax = value;
                OnPropertyChanged("ProgressBarMax");
                /*progressBar.Dispatcher.Invoke(new System.Action(() =>
                {
                    progressBar.Maximum = value;
                }));*/
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        public MainWindow()
        {
            InitializeComponent();
            progressBar.DataContext = this;
        }

        private void btnToDeleteFileNamePath_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.Button button = (System.Windows.Controls.Button)sender;
            //ExcelFileListBoxItem excelFileListBoxItem = (ExcelFileListBoxItem)button.DataContext;
            OpenFileDialog m_Dialog = new OpenFileDialog();
            m_Dialog.Filter = "Excel文件|*.xls;*.xlsx";
            //m_Dialog.SelectedPath = textBox.Text;
            bool? result = m_Dialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                tbToDeleteFileNamePath.Text = m_Dialog.FileName;
            }
        }

        private static List<string> getFiles(string[] allFiles, string key)
        {
            List<string> r = new List<string>();
            foreach (var str in allFiles)
            {
                if (str.Contains(key))
                    r.Add(str);
            }
            return r;
        }

        private async void BtnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string[] allFiles = Directory.GetFiles(tbPath.Text, "*.*", SearchOption.AllDirectories);
                List<string> manyFiles = new List<string>();
                List<string> noneFiles = new List<string>();
                List<string> deleteFiles = new List<string>();
                List<string[,]> result = await ExcelUtils.LoadData(tbToDeleteFileNamePath.Text);
                ProgressBarMax = result[0].GetUpperBound(0) + 1;
                Directory.CreateDirectory("backup");
                for (int i = 0; i <= result[0].GetUpperBound(0); i++)
                {
                    //string[] files = Directory.GetFiles(tbPath.Text, result[0][i, 0] + ".*", SearchOption.AllDirectories);
                    //for (int i = 0; i < files.Length; i++)
                    List<string> files = getFiles(allFiles, result[0][i, 0] + ".");
                    if (files.Count == 1)
                    {
                        //File.Delete(files[i]);
                        File.Move(files[0], "backup\\" + System.IO.Path.GetFileName(files[0]));
                        deleteFiles.Add(files[0]);
                    }
                    else if (files.Count == 0)
                        noneFiles.Add(result[0][i, 0]);
                    else
                        manyFiles.Add(result[0][i, 0]);
                    ProgressBarValue = i + 1;
                }

                Directory.CreateDirectory("output");
                File.WriteAllLines("output\\找到文件多于1个.txt", manyFiles.ToArray());
                File.WriteAllLines("output\\没找到文件.txt", noneFiles.ToArray());
                File.WriteAllLines("output\\已删除文件.txt", deleteFiles.ToArray());
                MessageBox.Show("删除完毕,确定无误可以清空backup文件夹");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            //result[0].
            //MessageBox.Show(result[0].GetUpperBound(0).ToString());
        }
    }
}
